# -*- coding: utf-8 -*-
import os

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

from google.appengine.ext.webapp import template

from modeling import model, package, primitiveType, enumeration, enumerationLiteral, class_, property, type, operation, parameter, comment, element, tag

from feedback import feedback
from counter import counter

from pagesConfiguration import pages

class MainPage(webapp.RequestHandler):
  def get(self):
      p = pages.Pages()
      
      path = os.path.join(os.path.dirname(__file__), 'html/index.html')
      
      self.response.out.write(p.getHeader(''))
      self.response.out.write(template.render(path, {}))
      self.response.out.write(p.getFoot(self.request.remote_addr))

class ShowChooseForm(webapp.RequestHandler):
    def get(self):
        
        p = pages.Pages()
        
        template_values = {'models': model.Model.all()}
        
        path = os.path.join(os.path.dirname(__file__), 'html/chooseModel.html')

        self.response.out.write(p.getHeader(':: Create or Edit a Metamodel '))
        self.response.out.write(template.render(path, template_values))
        self.response.out.write(p.getFoot(self.request.remote_addr))
        
class ImportModel(webapp.RequestHandler):
    def get(self):
        p = pages.Pages()
        
        path = os.path.join(os.path.dirname(__file__), 'html/importModel.html')
        
        self.response.out.write(p.getHeader(':: Import a XMI Metamodel '))
        self.response.out.write(template.render(path, {}))
        self.response.out.write(p.getFoot(self.request.remote_addr))        

class DebugDB(webapp.RequestHandler):
    def get(self):
        models = model.Model.all()
        registers = counter.Register.all()
        feeds = feedback.Feedback.all()
        
        self.response.out.write('<H1>MODELS ' + unicode(models.count()) + '</H1><BR><BR>')
        
        for m in models:
            self.response.out.write("Name = " + unicode(m.model_name) + "<br>")
            self.response.out.write("Key = " + unicode(m.key()) + "<br>")
            self.response.out.write(m.model_XMI)
            self.response.out.write("<br><br>")
        
        self.response.out.write('<H1>REGISTERS ' + unicode(registers.count()) + '</H1><BR><BR>')
        
        for r in registers:
            self.response.out.write("IP = " + unicode(r.ip) + "<br>")
            self.response.out.write("date = " + unicode(r.date) + "<br>")
            self.response.out.write("city = " + unicode(r.city) + "<br>")
            self.response.out.write("region = " + unicode(r.region) + "<br>")
            self.response.out.write("areaCode = " + unicode(r.areaCode) + "<br>")
            self.response.out.write("dmaCode = " + unicode(r.dmaCode) + "<br>")
            self.response.out.write("countryCode = " + unicode(r.countryCode) + "<br>")
            self.response.out.write("country = " + unicode(r.country) + "<br>")
            self.response.out.write("continentCode = " + unicode(r.continentCode) + "<br>")
            self.response.out.write("latitude = " + unicode(r.latitude) + "<br>")
            self.response.out.write("longitude = " + unicode(r.longitude) + "<br>")
            self.response.out.write("IP = " + unicode(r.ip) + "<br>")
            self.response.out.write("<br><br>")

        self.response.out.write('<H1>FEEDBACKS ' + unicode(feeds.count()) + '</H1><BR><BR>')
        
        for f in feeds:
            self.response.out.write("name = " + unicode(f.name) + "<br>")
            self.response.out.write("email = " + unicode(f.email) + "<br>")
            self.response.out.write("message = " + unicode(f.message) + "<br>")
            self.response.out.write("company = " + unicode(f.company) + "<br>")
            self.response.out.write("ip = " + unicode(f.ip) + "<br>")
            self.response.out.write("date = " + unicode(f.date) + "<br>")


application = webapp.WSGIApplication([('/', MainPage),
                                      ('/model', ShowChooseForm),
                                      ('/importModel.html', ImportModel),
                                      ('/model.import', model.ImportModelXMI),
                                      ('/model.create.model', model.CreateModel),
                                      ('/model.struct.model', model.LoadModelStructure),
                                      ('/model.load.model', model.LoadModelData),
                                      ('/model.save.model', model.SaveModelData),
                                      ('/model.export.model', model.ExportModelXMI),
                                      ('/model.create.package', package.CreatePackageData),
                                      ('/model.load.package', package.LoadPackageData),
                                      ('/model.save.package', package.SavePackageData),
                                      ('/model.create.primitivetype', primitiveType.CreatePrimiteTypeData),
                                      ('/model.load.primitivetype', primitiveType.LoadPrimitiveTypeData),
                                      ('/model.save.primitivetype', primitiveType.SavePrimitiveTypeData),
                                      ('/model.create.enumeration', enumeration.CreateEnumerationData),
                                      ('/model.load.enumeration', enumeration.LoadEnumerationData),
                                      ('/model.save.enumeration', enumeration.SaveEnumerationData),
                                      ('/model.create.enumerationliteral', enumerationLiteral.CreateEnumerationLiteralData),
                                      ('/model.load.enumerationliteral', enumerationLiteral.LoadEnumerationLiteralData),
                                      ('/model.save.enumerationliteral', enumerationLiteral.SaveEnumerationLiteralData),
                                      ('/model.create.class', class_.CreateClassData),
                                      ('/model.load.class', class_.LoadClassData),
                                      ('/model.save.class', class_.SaveClassData),
                                      ('/model.load.allclass', class_.LoadAllClass),
                                      ('/model.create.property', property.CreatePropertyData),
                                      ('/model.load.property', property.LoadPropertyData),
                                      ('/model.save.property', property.SavePropertyData),
                                      ('/model.load.allproperties', property.LoadAllProperties),
                                      ('/model.create.operation', operation.CreateOperationData),
                                      ('/model.load.operation', operation.LoadOperationData),
                                      ('/model.save.operation', operation.SaveOperationData),
                                      ('/model.create.parameter', parameter.CreateParameterData),
                                      ('/model.load.parameter', parameter.LoadParameterData),
                                      ('/model.save.parameter', parameter.SaveParameterData),
                                      ('/model.create.comment', comment.CreateCommentData),
                                      ('/model.load.comment', comment.LoadCommentData),
                                      ('/model.save.comment', comment.SaveCommentData),
                                      ('/model.create.tag', tag.CreateTagData),
                                      ('/model.load.tag', tag.LoadTagData),
                                      ('/model.save.tag', tag.SaveTagData),
                                      ('/model.load.alltypes', type.LoadAllTypes),
                                      ('/model.load.allelements', element.LoadAllElements),
                                      ('/feedback.html', feedback.PageFeedBack),
                                      ('/feedback.register', feedback.RegisterFeedBack),
                                      ('/debugdb', DebugDB)], 
                                     debug=True)    
def main():
    webapp.template.register_template_library('counter.tags')
    run_wsgi_app(application)

if __name__ == "__main__":
    main()